#include<bits/stdc++.h>
using namespace std;
#define int long long
#define llg long long
#define sig signed
#define rg register
#define sht short
#define endl "\n"
int ttt,n,a[200005],f[5005][5005],ff[2000005][11];
inline void solve1(){
    memset(f,0,sizeof(f));
    rg sig i,j;
    rg llg ans=0;
    for(i=1;i<=n;i++){
        for(j=1;j<i;j++){
            f[i][i-1]=max(f[i][i-1],f[i-1][j]);
            if(a[i]==a[j])f[i][i-1]=max(f[i][i-1],f[i-1][j]+a[i]);
            ans=max(ans,1ll*f[i][j]);
        }
        for(j=1;j<i;j++){
            f[i][j]=max(f[i][j],f[i-1][j]);
            if(a[i]==a[i-1])f[i][j]=max(f[i][j],f[i-1][j]+a[i]);
            ans=max(ans,1ll*f[i][j]);
        }
    }
    cout<<ans<<endl;
}
inline void solve2(){
    memset(ff,0xf0,sizeof(ff));
    rg sig i,j,k;
    rg llg ans=0;
    for(i=2;i<=n;i++){
        ff[i][a[i-1]]=0;
        for(j=1;j<=10;j++){
            ff[i][j]=max(ff[i][j],ff[i-1][j]);
            if(a[i]==a[i-1])ff[i][j]=max(ff[i][j],ff[i-1][j]+a[i]);

            ff[i][a[i-1]]=max(ff[i][a[i-1]],ff[i-1][j]);
            if(a[i]==j)ff[i][a[i-1]]=max(ff[i][a[i-1]],ff[i-1][j]+a[i]);

            ans=max(ans,ff[i][j]);
            //cout<<ff[i][j]<<" ";
        }
        //cout<<endl;
    }
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>ttt;
    while(ttt--){
        cin>>n;
        for(rg sig i=1;i<=n;i++)cin>>a[i];
        if(n<=5000)solve1();
        else solve2();
    }
    return 0;
}
